<html>
<head><meta charset="utf-8"><title>well-formed consts and trait bounds · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html">well-formed consts and trait bounds</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212182050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182050">(Oct 03 2020 at 17:45)</a>:</h4>
<p><span class="user-mention" data-user-id="121053">@varkor</span> I know that this code is super new, so is this behavior expected? I can't seem to use const generics to provide non-overlapping trait implementations:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(const_generics, const_evaluatable_checked)]</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">A</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">([</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span><span class="p">([])</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">N</span><span class="o">&gt;</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">T</span>: <span class="nb">Default</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">T</span>: <span class="nb">Copy</span><span class="p">,</span><span class="w"> </span><span class="c1">// I'm being lazy</span>
<span class="w">    </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">]</span>: <span class="nb">Sized</span><span class="p">,</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span><span class="p">([</span><span class="n">T</span>::<span class="n">default</span><span class="p">();</span><span class="w"> </span><span class="n">N</span><span class="p">])</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<div class="codehilite" data-code-language="none"><pre><span></span><code>error[E0080]: evaluation of constant value failed
  --&gt; src/lib.rs:15:9
   |
15 |     [T; N - 1]: Sized,
   |         ^^^^^ attempt to compute `0_usize - 1_usize`, which would overflow
</code></pre></div>



<a name="212182230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182230">(Oct 03 2020 at 17:48)</a>:</h4>
<p>for <code>const_evaluatable_checked</code> it's probably better to ask me</p>



<a name="212182236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182236">(Oct 03 2020 at 17:49)</a>:</h4>
<p>let me take a look here</p>



<a name="212182312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182312">(Oct 03 2020 at 17:50)</a>:</h4>
<p>eyyy, <span class="user-mention" data-user-id="124288">@oli</span> I found a situation where const eval eagerly emitting an error breaks stuff</p>



<a name="212182343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182343">(Oct 03 2020 at 17:51)</a>:</h4>
<p>so <span class="user-mention" data-user-id="116155">@Jake Goulding</span> this is probably one of the biggest hurdles for <code>const_evaluatable_checked</code> rn</p>



<a name="212182390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182390">(Oct 03 2020 at 17:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds/near/212182230">said</a>:</p>
<blockquote>
<p>for <code>const_evaluatable_checked</code> it's probably better to ask me</p>
</blockquote>
<p>Fair enough. It was even <a href="https://github.com/rust-lang/rust/issues/68436#issuecomment-702622679">your comment</a> that got me thinking about using it to solve the <code>Default</code> issue. But then I opened it again and saw varkor was the original author and then my brain stopped processing.</p>



<a name="212182400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182400">(Oct 03 2020 at 17:52)</a>:</h4>
<p>and I am not sure if we already want to fix it, as doing so requires quite big changes to const evaluation which are probably not even worth it seeing how far we are from even stabilizing <code>lazy_normalization_consts</code> and full <code>const_generics</code>.</p>



<a name="212182515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182515">(Oct 03 2020 at 17:55)</a>:</h4>
<p><a href="https://github.com/lcnr/rust/blob/a4783debe021c6d13ce3cf5167efe98c5baeaa26/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs#L175-L177">https://github.com/lcnr/rust/blob/a4783debe021c6d13ce3cf5167efe98c5baeaa26/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs#L175-L177</a></p>



<a name="212182741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182741">(Oct 03 2020 at 18:00)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span>, <span class="user-mention" data-user-id="120791">@RalfJ</span> how hard would it be to add a <code>try_const_eval</code> query with returns <code>Result&lt;..., ConstEvalErrorData&gt;</code> where <code>ConstEvalErrorData</code> can be used to build the relevant compile error if we need it. So most locations keep calling the <code>const_eval...</code> queries which internally now use <code>try_const_eval</code> and <code>is_const_evaluatable</code> now uses <code>try_const_eval</code> directly and only lazily emits an error if fulfill fails</p>



<a name="212182767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212182767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212182767">(Oct 03 2020 at 18:01)</a>:</h4>
<p>I personally am afraid of this change as it's probably somewhat invasive for a feature which is still quite far from stabilization, but I might be overestimating the required effort here</p>



<a name="212184056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212184056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212184056">(Oct 03 2020 at 18:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds/near/212182741">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="124288">oli</span>, <span class="user-mention silent" data-user-id="120791">RalfJ</span> how hard would it be to add a <code>try_const_eval</code> query with returns <code>Result&lt;..., ConstEvalErrorData&gt;</code> where <code>ConstEvalErrorData</code> can be used to build the relevant compile error if we need it. So most locations keep calling the <code>const_eval...</code> queries which internally now use <code>try_const_eval</code> and <code>is_const_evaluatable</code> now uses <code>try_const_eval</code> directly and only lazily emits an error if fulfill fails</p>
</blockquote>
<p>it requires the const eval error information to be serializable... which was the case in the past and it was quite painful, so I was pretty happy when <span class="user-mention" data-user-id="124288">@oli</span> changed things such that this is not required any more. However I forgot which data it was that I wanted to put in there which these trait bounds prevented... maybe <span class="user-mention" data-user-id="124288">@oli</span> remembers.<br>
Note that to reproduce the full error we would even need to encode the CTFE stacktrace, which can be quite large and which IIRC we never serialized in the past. This seems like quite the stress test for the query system, but maybe I am underestimating what kinds of data it can handle.</p>



<a name="212184144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212184144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212184144">(Oct 03 2020 at 18:31)</a>:</h4>
<p>also, potentially related: I have some cleanup planned in the area so that <code>const_eval_...</code> always raises a hard error when evaluation fails, instead of emitting lints sometimes. that will let us fix a bunch of bugs, reduce code duplication and remove a lot of fragile code in various places in the compiler.<br>
see <a href="https://github.com/rust-lang/rust/issues/71800">https://github.com/rust-lang/rust/issues/71800</a> and <a href="https://hackmd.io/Qvrj_eOFTkCHZrhJ7f1ItA">https://hackmd.io/Qvrj_eOFTkCHZrhJ7f1ItA</a></p>



<a name="212184451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212184451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212184451">(Oct 03 2020 at 18:38)</a>:</h4>
<p>hmm, we theoretically can add a bool as input to the <code>const_eval</code> query indicating if errors should be emitted without providing any info</p>



<a name="212184454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212184454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212184454">(Oct 03 2020 at 18:38)</a>:</h4>
<p>and then rerunning the query in fulfill if it previously failed to get the relevant error</p>



<a name="212184481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/well-formed%20consts%20and%20trait%20bounds/near/212184481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/well-formed.20consts.20and.20trait.20bounds.html#212184481">(Oct 03 2020 at 18:39)</a>:</h4>
<p>that also sounds like it's annoying though</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>